package com.fg.fgmall.common;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 * 统一的返回格式
 */
@Data
@Schema(
        title = "结果R",
        description = "返回的统一json格式"
)
public class R {

    private R() {}

    public static R getInstance() {
        return new R();
    }

    // 状态码
    @Schema(
            title = "状态码",
            description = "状态码",
            type = "Integer"
    )
    private Integer code;

    // 状态信息
    @Schema(
            title = "状态信息",
            description = "状态信息",
            type = "String"
    )
    private String msg;

    // 数据
    @Schema(
            title = "返回数据",
            description = "返回数据",
            type = "Object"
    )
    private Object data;

    public R code(Integer code) {
        this.code = code;
        return this;
    }

    public R msg(String msg) {
        this.msg = msg;
        return this;
    }

    public R data(Object data) {
        this.data = data;
        return this;
    }

    public static R ok(Object data) {
        return getInstance().code(200).msg("ok").data(data);
    }

    public static R error() {
        return getInstance().code(500).msg("error");
    }

    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}